import { WSClient } from './ws-client';
import { CONFIG } from '../utils/config';

// 定义WebSocket类型枚举
export enum WebSocketType {
    PRIVATE = 'private',
    BUSINESS = 'business',
    PUBLIC = 'public'
}

// WebSocket管理器，负责创建和管理不同类型的WebSocket连接
export class WebSocketManager {
    private clients: Map<WebSocketType, WSClient> = new Map();

    // 获取指定类型的WebSocket客户端
    public getClient(type: WebSocketType): WSClient {
        if (!this.clients.has(type)) {
            // 根据类型创建对应的客户端
            switch (type) {
                case WebSocketType.PRIVATE:
                    this.clients.set(type, new WSClient(CONFIG.PRIVATE_WS_URL));
                    break;
                case WebSocketType.BUSINESS:
                    this.clients.set(type, new WSClient(CONFIG.BUSINESS_WS_URL));
                    break;
                case WebSocketType.PUBLIC:
                    this.clients.set(type, new WSClient(CONFIG.PUBLIC_WS_URL));
                    break;
                default:
                    throw new Error(`Unknown WebSocket type: ${type}`);
            }
        }

        return this.clients.get(type)!;
    }

    // 关闭所有WebSocket连接
    public closeAll(disableReconnect: boolean = false): void {
        for (const client of this.clients.values()) {
            client.close(disableReconnect);
        }
        this.clients.clear();
    }

    // 判断是否所有客户端都已连接
    public allConnected(): boolean {
        if (this.clients.size === 0) return false;
        for (const client of this.clients.values()) {
            if (!client.isConnected()) return false;
        }
        return true;
    }
} 